package com.geostar.constructionland.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.common.entity.statistics.UnifiedDataQueryConfig;
import com.geostar.constructionland.statistics.mapper.UnifiedDataQueryMapper;
import com.geostar.constructionland.statistics.service.IUnifiedDataQueryConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * @Author makoto_zhu
 * @Date 2020-10-28 05:19:12
 * @Description
 **/
@Service
public class UnifiedDataQueryConfigServiceImpl extends ServiceImpl<UnifiedDataQueryMapper, UnifiedDataQueryConfig> implements IUnifiedDataQueryConfigService {

    @Resource
    private UnifiedDataQueryMapper unifiedDataQueryMapper;

    @Override
    public UnifiedDataQueryConfig getOne(String uniqueQueryId) {
        return this.lambdaQuery()
                .eq(UnifiedDataQueryConfig::getUniqueQueryId, uniqueQueryId)
                .orderByDesc(UnifiedDataQueryConfig::getModifiedTime)
                .list().stream()
                .findFirst().orElse(null);
    }


    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        if (id.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(id.split(","));
            unifiedDataQueryMapper.deleteBatchIds(versionIds);
        } else {
            unifiedDataQueryMapper.deleteById(id);
        }
    }

    public IPage<UnifiedDataQueryConfig> getDataQueryConfigForPage(int pageNo, int pageNum, String query_param_statistics_id) {
        IPage<UnifiedDataQueryConfig> analyseVersionIPage = new Page<>(pageNo, pageNum);
        LambdaQueryWrapper<UnifiedDataQueryConfig> queryWrapper = new LambdaQueryWrapper<UnifiedDataQueryConfig>().orderByDesc(UnifiedDataQueryConfig::getModifiedTime);
        if (StringUtils.isNotEmpty(query_param_statistics_id)) {
            queryWrapper.like(UnifiedDataQueryConfig::getUniqueQueryId, query_param_statistics_id);
        }
        return unifiedDataQueryMapper.selectPage(analyseVersionIPage, queryWrapper);
    }

    @Override
    public boolean saveDataQueryConfig(UnifiedDataQueryConfig dataQueryConfig) {
        Integer count = unifiedDataQueryMapper.selectCount(new LambdaQueryWrapper<UnifiedDataQueryConfig>().eq(UnifiedDataQueryConfig::getUniqueQueryId, dataQueryConfig.getUniqueQueryId()));
        if (count > 0) {
            return false;
        }
        unifiedDataQueryMapper.insert(dataQueryConfig);
        return true;
    }

    @Override
    public boolean updateDataQueryConfig(UnifiedDataQueryConfig dataQueryConfig) {
        Integer count = unifiedDataQueryMapper.selectCount(new LambdaQueryWrapper<UnifiedDataQueryConfig>().eq(UnifiedDataQueryConfig::getUniqueQueryId, dataQueryConfig.getUniqueQueryId()));
        if(StringUtils.isNotEmpty(dataQueryConfig.getId())) {
            count--;
        }
        if (count > 0) {
            return false;
        }
        unifiedDataQueryMapper.updateById(dataQueryConfig);
        return true;
    }
}
